﻿using Dm.filter;
using Simple.Http;
using Simple.ISysRepository;
using Simple.ISysService;
using Simple.SysRepository;
using Simple.Tools;
using Simple.Tools.Security;
using Simple.Tools.SnowFlake;
using Simple.Tools.Verify;
using SimpleSaas.Common;
using SimpleSaas.Common.Execption;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysService
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
    internal class SysUserService : ISysUserService
	{
		[Autowired]
		private IHttpHelper httpHelper { get; set; }
		/// <summary>
		/// 
		/// </summary>
		[Autowired]
		private ISysUserRepository sysUserRepository { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Autowired]
		private ISimpleMapper simpleMapper { get; set; }

		/// <summary>
		///  添加用户
		/// </summary>
		/// <param name="userDto"></param>
		public async Task AddAsync(SysUserDto userDto)
		{
			await sysUserRepository.AddAsync(userDto);
			#region todo 权限

			#endregion
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="loginUserDto"></param>
		/// <returns></returns>
		public async Task<IdentityLoginDto> LoginAsync(LoginUserDto loginUserDto)
		{
			HttpHelper.SetCurrentKey("AuthenticateService");

			var response = await httpHelper.PostAsync("/connect/token", new
			{
				client_id = "webclient",
				client_secret = "123456",
				grant_type = "password",
				username = loginUserDto.Account,
				password = loginUserDto.Password,
			}, HttpContextType.FormData);
			IdentityLoginDto identityLogin = new IdentityLoginDto();
			response.Then<IdentityLoginDto>(loginDto =>
			{
				identityLogin = loginDto;
			});

			if (SimpleCheck.IsNull(identityLogin) || identityLogin.Code != 200 || SimpleCheck.IsNullOrEmpty(identityLogin.AccessToken))
			{
				throw new BusinessException("登录失败!");
			}
			return identityLogin;
		}

		/// <summary>
		/// 登录接口
		/// </summary>
		/// <param name="loginUserDto"></param>
		/// <returns></returns>
		/// <exception cref="BusinessException"></exception>
		public async Task<LoginUserInfo> LoginByPasswordAsync(LoginUserDto loginUserDto)
		{
			var verify = loginUserDto.Verify();
			if (!verify.Item2)
			{
				throw new BusinessException(verify.Item1);
			}
			sysUserRepository.EnableLogger();

			SysUserEntity sysUser = await sysUserRepository.Where(user => !user.IsDeleted
			&& (loginUserDto.Account.Equals(user.Account)
			|| loginUserDto.Account.Equals(user.PhoneNumber))).FirstAsync();
			if (SimpleCheck.IsNull(sysUser))
			{
				throw new BusinessException("登录失败!");
			}

			if (!Security.AesDescrypt(sysUser.PasswordHash, sysUser.PasswordSalt, loginUserDto.Password))
			{
				throw new BusinessException("登录失败,密码填写错误!");
			}
			var userInfo = await sysUserRepository.Query(q =>
			{
				q.Where((user, tenant) => sysUser.Id.Equals(user.Id) && !tenant.IsDeleted);
			}).FirstAsync();

			if (SimpleCheck.IsNull(userInfo))
			{
				throw new BusinessException("登录失败!");
			}
			return userInfo;
		}
	}
}
